package com.briup.cms.web.controller;

import com.briup.cms.bean.Comment;
import com.briup.cms.exception.ServiceException;
import com.briup.cms.service.CommentService;
import com.briup.cms.util.JwtUtil;
import com.briup.cms.util.Result;
import com.briup.cms.util.ResultCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Api(tags = "Comment")
@RestController
@RequestMapping("/comment")
public class CommentController {

    @Autowired
    CommentService service;
    //注册一个评论
    @ApiOperation(value = "注册评论",notes = "传递一个json对象的字符串")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token",value = "JWT",
                    dataType = "String",required = true,
                    paramType = "header"),
    })
    @PostMapping("/saveOrUpdateComment")
    public Result saveOrUpdateComment(@RequestBody Comment comment){
        service.saveOrUpdateComment(comment);
        return Result.success(comment);
    }

    //根据评论id查找具体评论信息
    @ApiOperation(value = "id查找评论",notes = "传递一个id")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token",value = "JWT",
                    dataType = "String",required = true,
                    paramType = "header"),
    })
    @GetMapping("/findCommentById")
    public Result findCommentById(long id,HttpServletRequest request){
        String token = request.getHeader("token");
        Map<String, Object> info = JwtUtil.getInfo(token);
        int role = (int) info.get("role");
        if (role != 1){
            throw new ServiceException(ResultCode.ROLE_NO);
        }
        Comment comment = service.findCommentById(id);
        if (comment==null){
            throw new ServiceException(ResultCode.Comment_NOT_EXIST);
        }
        return Result.success(comment);
    }

    //分页查找所有评论
    @ApiOperation(value = "分页查找所有评论",notes = "传入第几页")
    @GetMapping("/findCommentAll")
    public Result findCommentAll(Integer pageNum){
        Page<Comment> page = service.findAll(pageNum,2);
        return Result.success(page);
    }

    @ApiOperation(value = "通过Id删除对象",notes = "传入要删除对象的Id")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token",value = "JWT",
                    dataType = "String",required = true,
                    paramType = "header"),
    })
    @DeleteMapping("/deleteCommentById/{id}")
    public Result deleteCommentById(@PathVariable Long id, HttpServletRequest request){
        String token = request.getHeader("token");
        Map<String, Object> info = JwtUtil.getInfo(token);
        int role = (int) info.get("role");
        if (role != 1){
            throw new ServiceException(ResultCode.ROLE_NO);
        }
        service.deleteCommentById(id);
        return Result.success("单个删除成功");
    }

    @ApiOperation(value = "通过Id批量删除对象",notes = "传入要删除对象的Id的集合")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token",value = "JWT",
                    dataType = "String",required = true,
                    paramType = "header"),
    })
    @PostMapping("/deleteCommentInBatch")
    public Result deleteCommentInBatch(@RequestBody List<Long> ids,HttpServletRequest request){
        String token = request.getHeader("token");
        Map<String, Object> info = JwtUtil.getInfo(token);
        int role = (int) info.get("role");
        if (role != 1){
            throw new ServiceException(ResultCode.ROLE_NO);
        }
        service.deleteCommentInBatch(ids);
        return Result.success("批量删除成功");
    }




    @ApiOperation(value = "根据资讯id查找资讯的所有评论，并分页显示评论",notes = "传入页码和每页记录数")
    @GetMapping("/findAllByArticleId")
    public Result findAllByArticleId(Long articleId,int pageNum){
        Page<Comment> allCommentsByArticleId = service.findAllByArticleId(articleId, pageNum, 2);
        return Result.success(allCommentsByArticleId);
    }
}
